package com.itheima.comment.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.comment.dao.CommentMapper;
import com.itheima.comment.service.CommentService;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.feign.ProductFeign;
import com.itheima.feign.UserFeign;
import com.itheima.pojo.Comment;
import com.itheima.pojo.User;
import com.itheima.util.IdWorker;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 查询全部列表
     *
     * @return
     */
    @Override
    public List<Comment> findAll() {
        return commentMapper.selectAll();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Comment findById(Integer id) {
        return commentMapper.selectByPrimaryKey(id);
    }


    /**
     * 用户增加点评
     * @param comment
     */
    @Override
    @Transactional
    public void add(Comment comment) {
        Integer userPhone = comment.getUserPhone();
        User user = (User) userFeign.findById(userPhone).getData();
        comment.setId((int) idWorker.nextId());
        comment.setCreateTime(new Date());
        comment.setUsername(user.getUsername());
        int total = comment.getServerScore()
                + comment.getEnvironmentScore()
                + comment.getIngredientScore()
                + comment.getTasteScore();
        comment.setTotal_score(total / 4);
        //更新商家表的星级
        productFeign.updateScore(comment.getProductId());
        commentMapper.insert(comment);
    }

    /**
     * 修改
     *
     * @param comment
     */
    @Override
    public void update(Comment comment) {
        commentMapper.updateByPrimaryKey(comment);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        commentMapper.deleteByPrimaryKey(id);
    }


    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<Comment> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return commentMapper.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Comment> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Comment>) commentMapper.selectAll();
    }

    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @Override
    public Page<Comment> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        return (Page<Comment>) commentMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // body
            if (searchMap.get("body") != null && !"".equals(searchMap.get("body"))) {
                criteria.andLike("body", "%" + searchMap.get("body") + "%");
            }
            // images
            if (searchMap.get("images") != null && !"".equals(searchMap.get("images"))) {
                criteria.andLike("images", "%" + searchMap.get("images") + "%");
            }
            // username
            if (searchMap.get("username") != null && !"".equals(searchMap.get("username"))) {
                criteria.andLike("username", "%" + searchMap.get("username") + "%");
            }

            // id
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // user_phone
            if (searchMap.get("userPhone") != null) {
                criteria.andEqualTo("userPhone", searchMap.get("userPhone"));
            }
            // 口味分数
            if (searchMap.get("tasteScore") != null) {
                criteria.andEqualTo("tasteScore", searchMap.get("tasteScore"));
            }
            // 环境分数
            if (searchMap.get("environmentScore") != null) {
                criteria.andEqualTo("environmentScore", searchMap.get("environmentScore"));
            }
            // 服务分数
            if (searchMap.get("serverScore") != null) {
                criteria.andEqualTo("serverScore", searchMap.get("serverScore"));
            }
            // 食材分数
            if (searchMap.get("ingredientScore") != null) {
                criteria.andEqualTo("ingredientScore", searchMap.get("ingredientScore"));
            }
            // 商家id
            if (searchMap.get("productId") != null) {
                criteria.andEqualTo("productId", searchMap.get("productId"));
            }

        }
        return example;
    }

}
